package com.check.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @Title: Dao
 * @Description:
 * @author: zyt
 * @date 2020年8月8日 下午4:12:10
 */

public class Dao {
    private static String url = "jdbc:mysql://localhost:3306/copy?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8";//数据库连接url
    private static String username = "admin";//数据库用户
    private static String password = "123456";//数据库密码
    private static Connection connection;
    public static Connection getConn() {
        try {
            //加载连接驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            connection = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }

    public int addCopy1(List<String> strs) {
        int i = 0;
        Connection conn = null;
        PreparedStatement ps = null;
        String sql="insert into copy1 ("
                + "t1,t2,t3,t4,t5,t6,t7,t8,t9,"
                + "t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,"
                + "t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,"
                + "t30,t31,t32,t33,t34,t35,t36,t37,t38"
                + ") values ("
                + " ?,?,?,?,?,?,?,?,?,?"
                + ",?,?,?,?,?,?,?,?,?,?"
                + ",?,?,?,?,?,?,?,?,?,?"
                + ",?,?,?,?,?,?,?,?"
                + ")";
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            int i1 = 1;
            for (int j = 1; j < strs.size(); j++) {
                ps.setString(i1++, strs.get(j));
            }
            i = ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                if(ps!=null) {
                    ps.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if(conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return i;
    }

    public int addCopy2(List<String> strs) {
        int i = 0;
        Connection conn = null;
        PreparedStatement ps = null;
        String sql="insert into copy1 (xuhao,name,idcard,sj,yx,rq,rqsj"
                + ") values (?,?,?,?,?,?,?)";
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            int i1 = 1;
            for (int j = 1; j < strs.size(); j++) {
                ps.setString(i1++, strs.get(i));
            }
            i = ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                if(ps!=null) {
                    ps.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if(conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return i;
    }


}
